﻿/** 
 * Copyright 2010 NUMKIT.COM (contact: opensource@numkit.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author 陈超
 * @version 1.0
 */
import flash.external.ExternalInterface;
class Place {
	static private var formatter:TextFormat;
	static private var defaultLink:String;
	static private var asyncMode:Boolean=false;
	static private var tipTF:TextField;
	private var x:Number;
	private var y:Number;
	private var id:Number;
	private var type:Number = 0;
	private var name:String = "";
	private var link:String = "";
	private var highlight:Boolean = false;
	private var outStand:Boolean=false;
	private var tField:TextField;
	private var mPic:MovieClip;
	private var scale:Number = 1;
	private var fontSize:Object;
	private var graphicMC:MovieClip;
	private var textMC:MovieClip;
	public function Place(node:XMLNode) {
		if (formatter == undefined) {
			formatter = new TextFormat();
			formatter.size = 12;
		}
		if (node.nodeName != "PL") {
			throw new Error("Not A Place Node!");
		}
		var idValue=parseInt(node.attributes["ID"]);
		if(!isNaN(idValue))
			id=idValue;
		x = undefined;
		y = undefined;
		for (var child:XMLNode = node.firstChild; child != null; child=child.nextSibling) {
			var nodeName:String = child.nodeName;
			if (nodeName == "Name") {
				this.name = child.firstChild.nodeValue;
			} else if (nodeName == "Link") {
				this.link = child.firstChild.nodeValue;
			} else if (nodeName == "Type") {
				this.type = parseInt(child.firstChild.nodeValue);
			} else if (nodeName == "X") {
				x = parseInt(child.firstChild.nodeValue);
			} else if (nodeName == "Y") {
				y = parseInt(child.firstChild.nodeValue);
			}
		}
		if (x == undefined || y == undefined) {
			throw new Error("Place Node XPos or YPos Not Set!");
		}
		fontSize = formatter.getTextExtent(this.name);
		//计算链接URL
		if(defaultLink!=undefined&&link==undefined){
			link=defaultLink;
			var pIndex:Number=defaultLink.indexOf("[Place]");
			if(pIndex!=-1){
				var aSplit=link.split("[Place]");
				link="";
				for(var j:Number=0;j<aSplit.length-1;j++){
					link+=aSplit[j]+name;
				}
				link+=aSplit[aSplit.length-1];
			}
			var iIndex:Number=defaultLink.indexOf("[ID]");
			if(iIndex!=-1){
				var aSplit=link.split("[ID]");
				link="";
				for(var j:Number=0;j<aSplit.length-1;j++){
					link+=aSplit[j]+id;
				}
				link+=aSplit[aSplit.length-1];
			}
		}
	}
	
	static public function setDefaultLink(theLink:String):Void{
		defaultLink=theLink;
	}
	
	static public function setTipTF(theTipTF:TextField):Void{
		tipTF=theTipTF;
	}
	public function drawGraphicPart(mc:MovieClip):Void {
		graphicMC=mc;
		//点
		if (highlight) {
			switch (type) {
			case 0 :
				mPic = mc.attachMovie("extern0", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 1 :
				mPic = mc.attachMovie("extern1", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 2 :
				mPic = mc.attachMovie("extern2", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 3 :
				mPic = mc.attachMovie("extern3", "mIcon",
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			default :
				throw new Error("Place type out of range!");
			}
		} else {
			switch (type) {
			case 0 :
				mPic = mc.attachMovie("redBall", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 1 :
				mPic = mc.attachMovie("greenBall", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 2 :
				mPic = mc.attachMovie("blueBall", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 3 :
				mPic = mc.attachMovie("purpleBall", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
				//building icon
			case 4 :
				mPic = mc.attachMovie("building1", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 5 :
				mPic = mc.attachMovie("building2", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 6 :
				mPic = mc.attachMovie("building3", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 7 :
				mPic = mc.attachMovie("building4", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
				//house icon
			case 8 :
				mPic = mc.attachMovie("house1", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 9 :
				mPic = mc.attachMovie("house2", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 10 :
				mPic = mc.attachMovie("house3", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			//traffic icon
			case 11 :
				mPic = mc.attachMovie("airport", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 12 :
				mPic = mc.attachMovie("car", "mIcon",
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 13 :
				mPic = mc.attachMovie("ship", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 14 :
				mPic = mc.attachMovie("train", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 15 :
				mPic = mc.attachMovie("gas", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			//other icon
			case 16 :
				mPic = mc.attachMovie("hotel", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 17 :
				mPic = mc.attachMovie("fork", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 18 :
				mPic = mc.attachMovie("coffee", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 19 :
				mPic = mc.attachMovie("clothing", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 20 :
				mPic = mc.attachMovie("store", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 21 :
				mPic = mc.attachMovie("edu", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 22 :
				mPic = mc.attachMovie("hospital", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 23 :
				mPic = mc.attachMovie("bank", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 24 :
				mPic = mc.attachMovie("music", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 25 :
				mPic = mc.attachMovie("forest", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			case 26 :
				mPic = mc.attachMovie("hill", "mIcon", 
					mc.getNextHighestDepth(), {_x:this.x, _y:this.y});
				break;
			default :
				throw new Error("Place type out of range!");
			}
		}
		//添加事件处理
		mc.tipTF=tipTF;
		mc.fontSize=fontSize;
		var place:Place=this;
		mc.place=place;
		mc.onRollOver = function():Void{
			var name:String=place.getName();
			if(name!=undefined&&name!=""&&place.getScale()<0.7){
				tipTF.text=name;
				tipTF._width=fontSize.textFieldWidth;
				tipTF._visible=true;
			}
		};
		mc.onRollOut = function():Void{
			tipTF.text="";
			tipTF._visible=false;
		};
		mc.onRelease = function():Void{
			if(Place.isAsyncMode()){
				var id:Number=place.getID();
				var name:String=place.getName();
				ExternalInterface.call("onPlaceClicked",name,id);
			}
			else{
				var link:String=place.getLink();
				if(link!=undefined&&link!=""){
					getUrl(link,"_blank");
				}
			}
		};
	}
	public function drawTextPart(mc:MovieClip):Void {
		textMC=mc;
		//fontSize = formatter.getTextExtent(name);//已经计算
		tField = mc.createTextField("tField", 
			mc.getNextHighestDepth(), 
			this.x+8, this.y-fontSize.textFieldHeight/2, 
			fontSize.textFieldWidth, fontSize.textFieldHeight);
		tField.selectable = false;
		tField.text = name;
		tField.setTextFormat(formatter);
		if (scale<0.7) {
			tField._visible = false;
		}
	}
	
	public function setScale(scale:Number):Void {
		if (this.scale != scale) {
			mPic._x = mPic._x*scale/this.scale;
			mPic._y = mPic._y*scale/this.scale;
			if(scale<1){
				mPic._xscale=scale*100*(outStand?2:1);
				mPic._yscale=scale*100*(outStand?2:1);
			}else{
				mPic._xscale=100*(outStand?2:1);
				mPic._yscale=100*(outStand?2:1);
			}
			tField._x = mPic._x+8;
			tField._y = mPic._y-fontSize.textFieldHeight/2;
			if (scale<0.7) {
				tField._visible = false;
			} else {
				tField._visible = true;
			}
			this.scale = scale;
		}
	}
	public function setHighlighted(highlighted:Boolean):Void {
		this.highlight = highlighted;
	}
	public function isHighlighted():Boolean {
		return highlight;
	}
	public function getLink():String {
		return link;
	}
	public function getName():String {
		return name;
	}
	public function getScale():Number{
		return scale;
	}
	public function getX():Number{
		return x;
	}
	public function getY():Number{
		return y;
	}
	public function getGraphicMC():MovieClip{
		return graphicMC;
	}
	public function getTextMC():MovieClip{
		return textMC;
	}
	
	static public function isAsyncMode():Boolean{
		return asyncMode;
	}
	
	static public function setAsyncMode(async:Boolean):Void{
		asyncMode=async;
	}
	public function getID():Number{
		if(!highlight){//如果不是附加点,id视为无效
			return undefined;
		}
		return id;
	}
	public function setOutStand(outStand:Boolean):Void{
		if(this.outStand!=outStand){
			if(scale<1){
				mPic._xscale=scale*100*(outStand?2:1);
				mPic._yscale=scale*100*(outStand?2:1);
			}else{
				mPic._xscale=100*(outStand?2:1);
				mPic._yscale=100*(outStand?2:1);
			}
			this.outStand=outStand;
		}
	}
	
	public function isOutStand():Boolean{
		return outStand;
	}
}
